package cn.laixueit.cart.service;

import cn.laixueit.cart.client.BrandClient;
import cn.laixueit.cart.client.CategoryClient;
import cn.laixueit.cart.client.GoodsClient;
import cn.laixueit.cart.client.SpecClient;
import cn.laixueit.cart.pojo.item.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsWebService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private SpecClient specClient;

    public Map<String ,Object> loadData(Long spuId){
        HashMap<String, Object> map = new HashMap<>();

        Spu spu = goodsClient.getSpuById(spuId).getBody();

        map.put("spu",spu);

        SpuDetail spuDetail = goodsClient.detail(spuId).getBody();
        map.put("spuDetail",spuDetail);

        List<Sku> skus = goodsClient.list(spuId).getBody();
        map.put("skus",skus);

        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());

        List<Map<String,Object>> categoryListMap = new ArrayList<>();
        for (Long cid : cids) {
            HashMap<String, Object> categoryMap = new HashMap<>();
            Category category = categoryClient.getCategoryById(cid).getBody();
            categoryMap.put("id",category.getId());
            categoryMap.put("name",category.getName());
            categoryListMap.add(categoryMap);
        }
        map.put("categorys",categoryListMap);

        Brand brand = brandClient.getBrandById(spu.getBrandId()).getBody();
        map.put("brand",brand);


        Collection<SpecGroup> specGroups = specClient.groups(spu.getCid3()).getBody();
        map.put("specGroups",specGroups);


        Collection<SpecParams> specParams = specClient.groups(null, spu.getCid3(), true, false).getBody();
        HashMap<Object, Object> paramMap = new HashMap<>();
        specParams.forEach(param ->{
            paramMap.put(param.getId(),param.getName());
        });
        map.put("paramMap",paramMap);

        return map;
    }
}
